package com.free.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * <p>
 * Title: Eclipse Plugin Development
 * </p>
 * <p>
 * Description: Free download
 * </p>
 * <p>
 * mail: ganshm@gmail.com
 * </p>
 */

public class ChooseColorDialogExample
{
    private Color color;
    
    /**
     * Runs the application
     */
    public void run()
    {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Color Chooser"); // shell的title
        createContents(shell);
        shell.pack(); // 以最合适的大小显示
        shell.open(); // 显示窗体
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
            { // true表示可能有很多事件需要处理，false表示暂时没有事件
                display.sleep(); // 等待事件发生
            }
        }
        // Dispose the color we created for the Label
        if (color != null)
        {
            color.dispose();
        }
        display.dispose(); // 结束进程
    }
    
    /**
     * Creates the window contents
     * 
     * @param shell the parent shell
     */
    private void createContents(final Shell shell)
    {
        shell.setLayout(new GridLayout(2, false)); // 两列，列之间不同宽
        
        // Start with Celtics green
        color = new Color(shell.getDisplay(), new RGB(0, 255, 0));
        
        // Use a label full of spaces to show the color
        final Label colorLabel = new Label(shell, SWT.NONE);
        colorLabel.setText("                              "); // 使用label来显示颜色
        colorLabel.setBackground(color);
        
        Button button = new Button(shell, SWT.PUSH);
        button.setText("Color...");
        button.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent event)
            {
                // 新建颜色对话框
                ColorDialog dlg = new ColorDialog(shell);
                
                // Set the selected color in the dialog from
                // user's selected color
                // 设置默认的颜色
                dlg.setRGB(colorLabel.getBackground().getRGB());
                
                // 设置标题字体
                dlg.setText("Choose a Color");
                
                // 打开对话框
                RGB rgb = dlg.open();
                if (rgb != null)
                { // 未选择颜色，直接关闭返回null
                    // Dispose the old color, create the
                    // new one, and set into the label
                    color.dispose();
                    color = new Color(shell.getDisplay(), rgb);
                    // 设置标签的颜色
                    colorLabel.setBackground(color);
                }
            }
        });
    }
    
    /**
     * The application entry point
     * 
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        new ChooseColorDialogExample().run();
    }
}
